package br.com.inventario.jdbc.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import br.com.inventario.jdbc.ConnectionFactory;
import br.com.inventario.jdbc.modelos.ParametroCampos;

public class ParametroCamposDAO {

	Connection connection;
	
	public ParametroCamposDAO(){
		try{
			this.connection = new ConnectionFactory().getConnection();
		} catch (SQLException e) {
			throw new RuntimeException (e);
		}
	}
		
	
	//Adiciona ParametroCampos
	public void adiciona(ParametroCampos oparametroscampos){
		String sql = "insert into tbParametroCampos(tbParametro_idParametro,nomeParametroCampos,tipoParametroCampos," +
				"posIniParametroCampos,posFinParametroCampos) values (?,?,?,?,?)";
		System.out.println(sql);
		try{
			PreparedStatement stmt = connection.prepareStatement(sql);
			stmt.setInt(1, oparametroscampos.getIdParametro());
			stmt.setString(2, oparametroscampos.getNome());
			stmt.setString(3, oparametroscampos.getTipo());
			stmt.setInt(4, oparametroscampos.getPosIni());
			stmt.setInt(5, oparametroscampos.getPosFin());
			stmt.execute();
			stmt.close();
		}catch (SQLException e){
			throw new RuntimeException(e);
		}
			
	}
	
	//Lista ParametroCampos
	public List<ParametroCampos> listaParametro(int idParametro){
		
		List<ParametroCampos> oParametroCampos = new ArrayList<ParametroCampos>();
		String sql = "select * from tbParametroCampos where tbParametro_idParametro=?";
		
		try {
			PreparedStatement stmt = connection.prepareStatement(sql);
			stmt.setInt(1, idParametro);
			ResultSet rs = stmt.executeQuery();
			while (rs.next()){
				ParametroCampos oParametroCampos2 = new ParametroCampos();
				oParametroCampos2.setId(rs.getInt("idParametroCampos"));
				oParametroCampos2.setIdParametro(rs.getInt("tbParametro_idParametro"));
				oParametroCampos2.setNome(rs.getString("nomeParametroCampos"));
				oParametroCampos2.setTipo(rs.getString("tipoParametroCampos"));
				oParametroCampos2.setPosIni(rs.getInt("posIniParametroCampos"));
				oParametroCampos2.setPosFin(rs.getInt("posFinParametroCampos"));
				oParametroCampos.add(oParametroCampos2);
			}
			stmt.close();
			rs.close();
			return oParametroCampos;
		} catch (SQLException e){
			throw new RuntimeException(e);
		}
			
	}
	
	
}


